class_name InventorySlot
extends Control

## 背包槽位类，处理单个物品槽的显示和交互
## 支持拖拽、点击、物品显示等功能

signal slot_clicked(slot: InventorySlot)
signal slot_double_clicked(slot: InventorySlot)
signal item_dropped(from_slot: InventorySlot, to_slot: InventorySlot)


@export var slot_size: Vector2 = Vector2(50, 50)
@export var border_color: Color = Color.GRAY
@export var selected_color: Color = Color.YELLOW

var item_data: ItemData
var item_count: int = 0
var is_selected: bool = false
var slot_index: int = -1

@onready var background: ColorRect
@onready var item_icon: TextureRect
@onready var count_label: Label
@onready var border: ColorRect

func _ready():
	setup_ui()
	setup_drag_and_drop()

## 设置UI组件
func setup_ui():
	# 设置槽位大小
	custom_minimum_size = slot_size
	size = slot_size
	
	# 创建背景
	background = ColorRect.new()
	background.color = Color(0.8, 0.8, 0.8, 0.9)
	background.size = slot_size
	background.mouse_filter = Control.MOUSE_FILTER_IGNORE
	add_child(background)
	
	# 创建边框
	border = ColorRect.new()
	border.color = Color.TRANSPARENT
	border.size = slot_size
	border.mouse_filter = Control.MOUSE_FILTER_IGNORE
	add_child(border)
	
	# 创建物品图标
	item_icon = TextureRect.new()
	item_icon.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL
	item_icon.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
	item_icon.size = slot_size * 0.8
	item_icon.position = slot_size * 0.1
	item_icon.mouse_filter = Control.MOUSE_FILTER_IGNORE
	add_child(item_icon)
	
	# 创建数量标签
	count_label = Label.new()
	count_label.text = ""
	count_label.add_theme_font_size_override("font_size", 12)
	count_label.add_theme_color_override("font_color", Color.WHITE)
	count_label.add_theme_color_override("font_shadow_color", Color.BLACK)
	count_label.add_theme_constant_override("shadow_offset_x", 1)
	count_label.add_theme_constant_override("shadow_offset_y", 1)
	count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
	count_label.vertical_alignment = VERTICAL_ALIGNMENT_BOTTOM
	count_label.size = slot_size
	count_label.mouse_filter = Control.MOUSE_FILTER_IGNORE
	add_child(count_label)
	
	# 确保父控件可以接收鼠标事件
	mouse_filter = Control.MOUSE_FILTER_PASS

## 设置拖拽功能
func setup_drag_and_drop():
	mouse_entered.connect(_on_mouse_entered)
	mouse_exited.connect(_on_mouse_exited)
	gui_input.connect(_on_gui_input)

## 设置物品数据
func set_item(data: ItemData, count: int = 1):
	item_data = data
	item_count = count
	update_display()

## 清空槽位
func clear_slot():
	item_data = null
	item_count = 0
	update_display()

## 更新显示
func update_display():
	if item_data == null:
		item_icon.texture = null
		count_label.text = ""
		tooltip_text = ""
	else:
		# 加载物品图标
		if item_data.icon_path != "":
			var texture = load(item_data.icon_path)
			if texture:
				item_icon.texture = texture
		
		# 显示数量
		if item_count > 1:
			count_label.text = str(item_count)
		else:
			count_label.text = ""
		
		# 设置工具提示
		tooltip_text = item_data.get_display_name() + "\n" + item_data.get_full_description()
	
	update_border()

## 更新边框显示
func update_border():
	if is_selected:
		border.color = selected_color
	else:
		border.color = border_color

## 设置选中状态
func set_selected(selected: bool):
	is_selected = selected
	update_border()

## 检查是否为空槽位
func is_empty() -> bool:
	return item_data == null

## 检查是否可以添加物品
func can_add_item(data: ItemData, count: int = 1) -> bool:
	if is_empty():
		return true
	
	if item_data == data and item_data.can_stack():
		return item_count + count <= item_data.max_stack_size
	
	return false

## 添加物品
func add_item(data: ItemData, count: int = 1) -> int:
	if not can_add_item(data, count):
		return count  # 返回无法添加的数量
	
	if is_empty():
		set_item(data, count)
		return 0
	else:
		var can_add = min(count, item_data.max_stack_size - item_count)
		item_count += can_add
		update_display()
		return count - can_add

## 移除物品
func remove_item(count: int = 1) -> int:
	if is_empty():
		return 0
	
	var removed = min(count, item_count)
	item_count -= removed
	
	if item_count <= 0:
		clear_slot()
	else:
		update_display()
	
	return removed

## 鼠标进入事件
func _on_mouse_entered():
	if not is_empty():
		border.color = Color.WHITE

## 鼠标离开事件
func _on_mouse_exited():
	update_border()

## GUI输入事件
func _on_gui_input(event: InputEvent):
	if event is InputEventMouseButton:
		var mouse_event = event as InputEventMouseButton
		if mouse_event.pressed and mouse_event.button_index == MOUSE_BUTTON_LEFT:
			# 双击检测
			if mouse_event.double_click:
				slot_double_clicked.emit(self)
			else:
				slot_clicked.emit(self)

func _get_drag_data(_position: Vector2):
	if is_empty():
		return null

	# 创建拖拽预览
	var preview = Control.new()
	var icon = TextureRect.new()
	icon.texture = item_icon.texture
	icon.size = slot_size * 0.8
	# 设置图标位置使其居中
	icon.position = -icon.size / 2
	preview.add_child(icon)
	set_drag_preview(preview)

	return {
		"type": "item_drag",
		"source_slot": self
	}

func _can_drop_data(_position: Vector2, data) -> bool:
	return data is Dictionary and data.has("type") and data["type"] == "item_drag"

## 接收拖拽数据
func _drop_data(_position: Vector2, data):
	var from_slot = data["source_slot"] as InventorySlot
	if from_slot != self:
		item_dropped.emit(from_slot, self)
